<template>
  <div class="pad-20 bgc-default">
    <div class="bg-color-white pad-l-20 pad-r-20 pad-t-20">
      <div>
        <el-button @click="$router.push({ path: '/system/role/detail/index' })" type="primary">新增</el-button>
      </div>
      <!-- 操作区域 -->
      <div class="top-operate-default mar-t-20">
        <div class="search-item">
          <label>搜索角色：</label>
          <el-input @keyup.enter.native="handleSearch" v-model="listQuery.keyword" placeholder="角色名" style="width: 200px"></el-input>
          <label class="mar-l-20">状态：</label>
          <el-select v-model="listQuery.status" placeholder="状态">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(val, key) in dict.status" :key="key" :label="val" :value="key"></el-option>
          </el-select>
          <label class="mar-l-20">所属子系统：</label>
          <el-select v-model="typeSelect" placeholder="所属子系统">
            <el-option  key="0" label="全部" value="0"> </el-option>
            <el-option  key="1" label="企业子系统" value="1"></el-option>
            <el-option  key="2" label="供应商子系统" value="2"></el-option>
            <el-option  key="3" label="微客子系统" value="3"></el-option>
            <el-option  key="4" label="仓库子系统" value="4"></el-option>
          </el-select>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <div class="pad-t-15 pad-b-15 font-14 v-align-c">
        <el-checkbox class="mar-l-14 mar-r-0" v-model="allChecked" @change="selectAll"><span class="mar-l-20">当页全选</span></el-checkbox>
        <span class="pad-l-4 pad-r-4">|</span>
        <div class="width-80">已选{{multipleSelection.length}}项</div>
        <el-button class="batch-btn" @click="handleBatchEnable">批量启用</el-button>
        <el-button class="batch-btn" @click="handleBatchDisable">批量禁用</el-button>
        <el-button class="batch-btn" @click="handleBatchDelete">批量删除</el-button>
      </div>
      <!-- 内容列表 -->
      <div class="table-container">
        <el-table ref="multipleTable" :data="listData" @selection-change="handleSelectionChange" v-loading="listLoading"
                  element-loading-text="加载中" header-cell-class-name="default-header" row-class-name="default-row"
                  style="width: 100%">
          <el-table-column width="55px" type="selection"></el-table-column>
          <el-table-column label="角色" prop="name">
            <template slot-scope="scope">
              <div class="cursor-p" style="color:#155bd4;" @click="handleUpdate(scope.row)">{{scope.row.name}}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作员数量"  prop="userNum">
          </el-table-column>
          <el-table-column label="所属子系统" >
            <template slot-scope="scope">
              <span v-if="scope.row.firstFlag">企业子系统</span>
              <span v-if="scope.row.supplierFlag">供应商子系统</span>
              <span v-if="scope.row.memberFlag">微客子系统</span>
              <span v-if="scope.row.warehouseFlag">仓库子系统</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status" >
            <template slot-scope="scope">
              <span>{{scope.row.status == 1 ? '启用中' : '禁用'}}</span>
            </template>
          </el-table-column>
          <el-table-column width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="handleUpdate(scope.row)">编辑</el-button>
              <el-button type="text" size="medium" @click="handleDisable(scope.row)">{{scope.row.status != 1 ? '启用' : '禁用'}}</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="handleDelete(scope.row)"
              >
                <el-button slot="reference" class="mar-l-10" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                      :current-page="listQuery.currentPage"
                      :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.size"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>


<style scoped>

</style>

<script>
  import { getNameByPath } from '@/utils/get-name-by-path' // 不要删除，根据URL获取文件名
  import { getRoleList, deleteRole, updateRoleStatus, batUpdateStatus, batDelete } from '@/api/system/role'

  export default {
    name: getNameByPath(),
    data() {
      return {
        listData: [],
        permissionList: [],
        total: null,
        listLoading: true,
        keyword: '',
        typeSelect: '0',
        multipleSelection: [],
        listQuery: {
          currentPage: 1,
          size: 10,
          keyword: '',
          status: ''
        },
        role: {
          status: ''
        },
        dict: {},
        allChecked: false
      }
    },
    // 过滤器
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          2: 'danger'
        }
        return statusMap[status]
      }
    },
    // 初始化页面
    created() {
      this.initRoleList()
    },
    methods: {
      initRoleList() {
        this.listLoading = true
        this.listQuery.userCode = this.getUserCode()
        this.listQuery['firstFlag'] = ''
        this.listQuery['supplierFlag'] = ''
        this.listQuery['memberFlag'] = ''
        if (this.typeSelect === '1') {
          this.listQuery['firstFlag'] = 1
        }
        if (this.typeSelect === '2') {
          this.listQuery['supplierFlag'] = 1
        }
        if (this.typeSelect === '3') {
          this.listQuery['memberFlag'] = 1
        }
        if (this.typeSelect === '4') {
          this.listQuery['warehouseFlag'] = 1
        }
        getRoleList(this.listQuery).then(response => {
          this.listData = response.records
          this.dict = response.dict
          this.total = response.total
          this.listLoading = false
        })
      },
      handleSearch() {
        this.listQuery.currentPage = 1
        this.initRoleList()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.initRoleList()
      },
      handleCurrentChange(val) {
        this.listQuery.currentPage = val
        this.initRoleList()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
        if (this.multipleSelection.length === this.listData.length) {
          this.allChecked = true
        } else {
          this.allChecked = false
        }
      },
      // 启用、禁用
      handleDisable(row) {
        // 超级管理员不用设置权限
        if (row.adminFlag) {
          this.$message({
            type: 'error',
            message: '超级管理员不能禁用!'
          })
          return
        }
        let status = row.status
        status = status === 1 ? 2 : 1
        updateRoleStatus(row.subRoleId, status).then(response => {
          if (response.isError) return
          this.$message({
            type: 'success',
            message: '更新成功!'
          })
          this.initRoleList()
        })
      },
      // 编辑
      handleUpdate(row) {
        // 超级管理员不用设置权限
        if (row.adminFlag) {
          this.$message({
            type: 'error',
            message: '超级管理员不能编辑!'
          })
          return
        }
        this.$router.push({ path: '/system/role/detail/index', query: row })
      },
      // 删除
      handleDelete(row) {
        // 超级管理员不能删除
        if (row.adminFlag) {
          this.$message({
            type: 'error',
            message: '超级管理员不能删除!'
          })
          return
        }
        this.$confirm('此操作将删除该记录，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteRole(row.subRoleId).then((response) => {
            if (response.isError) {
              return
            }
            this.initRoleList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        }).catch(() => {})
      },
      // 批量启用
      handleBatchEnable(row) {
        const multipleSelection = this.multipleSelection
        if (multipleSelection.length === 0) {
          this.$message({
            type: 'error',
            message: '请先选择角色!'
          })
          return
        }
        const roleIds = []
        multipleSelection.forEach((e) => {
          roleIds.push(e.subRoleId)
        })
        batUpdateStatus(roleIds, 1).then(response => {
          if (response.isError) return
          this.$message({
            type: 'success',
            message: '成功!'
          })
          this.initRoleList()
        })
      },
      // 批量禁用
      handleBatchDisable(row) {
        const multipleSelection = this.multipleSelection
        if (multipleSelection.length === 0) {
          this.$message({
            type: 'error',
            message: '请先选择角色!'
          })
          return
        }
        const roleIds = []
        multipleSelection.forEach((e) => {
          roleIds.push(e.subRoleId)
        })
        batUpdateStatus(roleIds, 2).then(response => {
          if (response.isError) return
          this.$message({
            type: 'success',
            message: '成功!'
          })
          this.initRoleList()
        })
      },
      // 批量删除
      handleBatchDelete(row) {
        const multipleSelection = this.multipleSelection
        if (multipleSelection.length === 0) {
          this.$message({
            type: 'error',
            message: '请先选择角色!'
          })
          return
        }
        this.$confirm('此操作将批量删除角色，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const roleIds = []
          multipleSelection.forEach((e) => {
            roleIds.push(e.subRoleId)
          })
          batDelete(roleIds).then(response => {
            if (response.isError) return
            this.$message({
              type: 'success',
              message: '成功!'
            })
            this.initRoleList()
          })
        }).catch(() => {
        })
      },
      selectAll() {
        if (this.multipleSelection.length === 0 || this.multipleSelection.length < this.listData.length) {
          this.listData.forEach(item => {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      // 重置搜索
      resetSearch() {
        this.listQuery.currentPage = 1
        this.listQuery.keyword = ''
        this.listQuery.status = ''
        this.typeSelect = '0'
        this.initRoleList()
      }
    },
    mounted() {

    }
  }
</script>
